package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.dao.ActivityColumnDao;
import com.ytjj.qmyx.admin.service.ActivityColumnService;
import com.ytjj.qmyx.mall.mapper.ActivityColumnMapper;
import com.ytjj.qmyx.mall.model.ActivityColumn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 *
 * @Version: 1.0
 */
@Service
public class ActivityColumnServiceImpl implements ActivityColumnService {

    @Resource
    private ActivityColumnDao activityColumnDao;
    @Resource
    private ActivityColumnMapper activityColumnMapper;

    @Override
    public List<ActivityColumn> getTitleByTitle(String type) {
        return activityColumnDao.getTitleByTitle(type);
    }

    @Override
    public List<ActivityColumn> getType() {
        return activityColumnDao.getType();
    }

    @Override
    public CommonResult list(Integer id, String type, String title, Integer isShow, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ActivityColumn> list = activityColumnDao.selectByCondition(id,type,title,isShow,null);
        return CollectionUtils.isEmpty(list) ? CommonResult.success(new ArrayList<>()) : CommonResult.success(CommonPage.restPage(list));
    }

    @Override
    public CommonResult editColumn(ActivityColumn activityColumn) {
        int count = 0;
        if (null == activityColumn.getId()){
            activityColumn.setCreateTime(new Date());
            activityColumn.setStatus((byte) 1);
            count = activityColumnMapper.insert(activityColumn);
        }else {
            activityColumn.setUpdateTime(new Date());
            count = activityColumnMapper.updateByPrimaryKeySelective(activityColumn);
        }
        return count > 0 ? CommonResult.success() : CommonResult.failed();
    }
}
